package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWebContextNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWebContextParameter;

public class WebContextNodeLoader extends NodeLoader {

	public List<Shape> loadNodes(Element root) {

		NodeList webContextNodes = root
				.getElementsByTagName(SaturnWebContextNode.WEB_CONTEXT);

		List<Shape> shapes = new ArrayList<Shape>(webContextNodes.getLength());

		for (int i = 0; i < webContextNodes.getLength(); ++i) {
			SaturnWebContextNode saturnWebContextNode = new SaturnWebContextNode();

			Node webContextNode = webContextNodes.item(i);
			String shapeId = super.getNodeAttributeValue(webContextNode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);
			String nextId = super.getNodeAttributeValue(webContextNode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);

			String name = super.getNodeElementValue(webContextNode,
					SaturnWebContextNode.WEB_CONTEXT_NAME);
			String description = super.getNodeElementValue(webContextNode,
					SaturnWebContextNode.WEB_CONTEXT_DESCRIPTION);
			
			List<SaturnWebContextParameter> webParameters = super.getWebParameters((webContextNode));

			Point location = super.getLocation(webContextNode);
			Dimension size = new Dimension(50, 70);
			
			saturnWebContextNode.setNode(webContextNode);
			saturnWebContextNode.setId(shapeId);
			saturnWebContextNode.setLocation(location);
			saturnWebContextNode.setName(name);
			saturnWebContextNode.setNextId(nextId);
			saturnWebContextNode.setDescription(description);
			saturnWebContextNode.setSaturnWebContextParameterList(webParameters);
			saturnWebContextNode.setSize(size);
			
			shapes.add(saturnWebContextNode);
		}

		return shapes;
	}
}
